package wanba.wanbatv.com.wudao.util;

import android.media.MediaPlayer;
import android.util.Log;

import com.squareup.okhttp.Callback;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;

import java.io.IOException;

/**
 * Created by Forcs on 15/6/10.
 */
public class WanbaMediaPlayer extends MediaPlayer {

    private OkHttpClient mHttpClient = null;

    public interface OnCheckFinishCallback {

        public void onCheckFinish(boolean success);
    }

    public WanbaMediaPlayer() {
        super();
    }

    /**
     * 设置远程播放地址
     * @param url 播放地址
     * @throws IOException
     */
    public void setUrl(String url, OnCheckFinishCallback callback) throws IOException {
        setUrl(url, true, callback);
    }

    /**
     * 设置远程播放地址，并配置是否需要检查重定向地址
     * @param url 播放地址
     * @param check true 通过内部的HttpClient检查重定向地址，否则，不检查
     * @throws IOException
     */
    public void setUrl(final String url, boolean check, final OnCheckFinishCallback callback) throws IOException {
        if (check) {    //检查地址是否重定向
            if (mHttpClient == null) {
                mHttpClient = new OkHttpClient();
            }
            //请求
            Request request = new Request.Builder()
                    .url(url)
                    .build();
            mHttpClient.newCall(request)
                    .enqueue(new Callback() {
                        @Override
                        public void onFailure(Request request, IOException e) {
                            Log.d("WanbaMedia","@@ OnFailure");
                            if (callback != null) {
                                callback.onCheckFinish(false);
                            }
                        }

                        @Override
                        public void onResponse(Response response) throws IOException {
                            String data=url;
                            if (response.isRedirect()) {    //如果重定向，就用重定向地址进行播放
                                data=response.header("Location", data);
                            }
                            try {
                                Log.d("WanbaMedia","@@@ play"+data);
                                setDataSource(data);
                                Log.d("WanbaMedia","@@@ play ok");
                            }catch (IllegalStateException e){
                                Log.d("Simple==","state==");
                                e.printStackTrace();
                            }catch (IOException e){
                                Log.d("WanbaMedia","@@@ play error");
                                e.printStackTrace();
                            }

                            if (callback != null) {
                                callback.onCheckFinish(true);
                            }
                        }
            });


        } else {    //不检查就直接播放
            setDataSource(url);
            if (callback != null) {
                callback.onCheckFinish(true);
            }
        }
    }

}
